#include "tempiritorno.h"
#include <cmath>
#include <QString>
#include "QtDebug.h"

string TempiRitorno::m_objname = "TempiRitorno";

TempiRitorno::TempiRitorno() :
    Customizable(false)
{
           m_isAlt = false;
    m_isChecked = false;
           m_isDat = false;
        m_isSolved = false;
           m_value = -1;
}

TempiRitorno::TempiRitorno(const PKeyList* plist) :
    Customizable(false),
    PKeyItem(plist->getListId()),
    PRef(plist)
{
           m_isAlt = false;
    m_isChecked = false;
           m_isDat = false;
        m_isSolved = false;
           m_value = -1;
}

TempiRitorno::TempiRitorno(const PKeyItem& key) :
    Customizable(false),
    PKeyItem(key)
{
           m_isAlt = false;
    m_isChecked = false;
           m_isDat = false;
        m_isSolved = false;
           m_value = -1;
}

TempiRitorno::TempiRitorno (const TempiRitorno& tr) :
    PKey(tr),
    PItem(tr),
    PRef(tr),
    tp_cu(tr.tp_cu),
    tp_co(tr.tp_co),
    tp_sls(tr.tp_sls)
{
    m_isAlt = tr.m_isAlt;
    m_isChecked = tr.m_isChecked;
    m_isDat = tr.m_isDat;
    m_isSolved = tr.m_isSolved;
    m_value = tr.m_value;
    Customizable::setCustom(tr.isCustom());
}

TempiRitorno& TempiRitorno::operator = (const TempiRitorno& tr)
{
    setKeyStr(tr.getKeyStr());
    setDescr(tr.getDescr());

    if ( tr.getTabRef() != EXA_TAB_NULL )
    {
        setTabRef(tr.getTabRef());
    }

    PRef::operator = ( tr );

    tp_co = tr.tp_co;
    tp_cu = tr.tp_cu;
    tp_sls = tr.tp_sls;

    m_isAlt = tr.m_isAlt;
    m_isChecked = tr.m_isChecked;
    m_isDat = tr.m_isDat;
    m_isSolved = tr.m_isSolved;

    m_value = tr.m_value;

    Customizable::setCustom(tr.isCustom());
    return *this;
}

TempiRitorno::TempiRitorno(const TpClasseUso& cu, const TpCostruzione& co, const TpSLsismico& sls, const PKeyItem& key) :
    PReport(m_objname),
    Customizable(false),
    PKey(key),
    PItem(key)
{
    tp_co = co;
    tp_cu = cu;
    tp_sls = sls;
    m_value = -1;
}

TempiRitorno::TempiRitorno(const TpClasseUso& cu, const TpCostruzione& co, const TpSLsismico& sls, const string& name) :
    PReport(m_objname),
    Customizable(false)
{
    tp_co = co;
    tp_cu = cu;
    tp_sls = sls;

    m_value = -1;

    if (!name.empty()) setDescr(name);
}

TempiRitorno::TempiRitorno(const double& tr, const PKeyItem& key) :
    Customizable(true),
    PKeyItem(key),
    PReport(m_objname)
{
    if (tr > 0 && tr <= 2475)
        m_value = tr;
    else
        m_value = -1;
}

TempiRitorno::TempiRitorno(const double& tr, const string& name) :
    Customizable(false),
    PReport(m_objname)
{
    if (!name.empty()) setDescr(name);

    if (tr > 0 && tr <= 2475)
        m_value = tr;
    else
        m_value = -1;
}

bool TempiRitorno::solve(void)
{
    if (!m_isAlt)
	{
        if (!m_isChecked)
		{
            m_isAlt = true;
            m_isSolved = false;
			return false;
		}
		else
		{
            m_vr = tp_co.getFlagValue() * tp_cu.getFlagValue();
            m_value = - m_vr / log(1-tp_sls.getFlagValue()/100);
            Customizable::setCustom(false);
            m_isAlt = false;
            m_isSolved = true;
			return true;
		}
	}
	else return false;
}

bool TempiRitorno::solverCheck(void)
{
    m_isSolved = false;
    m_isChecked = true;
	if (tp_co.isSetFlag() && tp_cu.isSetFlag() && tp_sls.isSetFlag())
	{
        m_isDat = true;
        m_isAlt = false;
		return true;
	}
	return false;
}

double TempiRitorno::solveVr( bool* ok )
{
    bool* is_ok = new bool;
    if (tp_co.isSetFlag() && tp_cu.isSetFlag())
    {
        m_vr = tp_co.getFlagValue() * tp_cu.getFlagValue();
        *is_ok = true;
    }
    else
    {
        *is_ok = false;
    }
    if ( ok != 0 ) *ok = *is_ok;
    return ( m_vr );
}

bool TempiRitorno::isComplete(void) const
{
    if (isCustom() == false)
    {
    if (tp_co.isSetFlag() && tp_cu.isSetFlag() && tp_sls.isSetFlag() && !getDescr().empty() && !getKeyStr().empty())
		return(true);
	else
		return(false);
    }
    else
    {
        if (m_value != -1  && !getDescr().empty() && !getKeyStr().empty())
            return(true);
        else
            return(false);
    }
}

int TempiRitorno::setFlagValue(const double& value)
{
    if (value > 0 && value <= 2475)
    {
        m_value = value;
        Customizable::setCustom(true);
        tp_co.setFlag(TpCostruzione::NONE);
        tp_cu.setFlag(TpClasseUso::NONE);
        tp_sls.setFlag(TpSLsismico::NONE);
        return 1;
    }
    else if (value == -1)
    {
        // setCustom(false);
        m_value = value;
        return 0;
    }
    return 0;
}

void TempiRitorno::setCustom(const bool& cust)
{
    Customizable::setCustom(cust);
    tp_co.setFlag(TpCostruzione::NONE);
    tp_cu.setFlag(TpClasseUso::NONE);
    tp_sls.setFlag(TpSLsismico::NONE);
}

void TempiRitorno::reportMake(const string& objname) const
{
    if (getReportCurrentStyle() == STYLE_VERBOSE)
    {
        PReport::resetReportOpt();
        PReport::setReportOpt("SUB",true);
        PKeyItem::reportMake();
        PReport::setReportOpt("SUB",false);
        moveLineInBlock(string("TIPO DI COSTRUZIONE"));
        addInLine("val",tp_co.getFlagValue());
        addInLine("set",tp_co.isSetFlag());
        addInLine("cus",tp_co.isCustom());
        addInLine("str",tp_co.getFlagStr());
        moveLineInBlock();
        moveLineInBlock(string("TIPO DI CLASSE D'USO"));
        addInLine("val",tp_cu.getFlagValue());
        addInLine("set",tp_cu.isSetFlag());
        addInLine("cus",tp_cu.isCustom());
        addInLine("str",tp_cu.getFlagStr());
        moveLineInBlock();
        moveLineInBlock(string("STATO LIMITE SISMICO"));
        addInLine("val",tp_sls.getFlagValue());
        addInLine("set",tp_sls.isSetFlag());
        addInLine("cus",tp_sls.isCustom());
        addInLine("str",tp_sls.getFlagStr());
        moveLineInBlock();
        addInLine("tempo di ritorno Tr",getFlagValue());
        moveLineInBlock();
    }
    else if (getReportCurrentStyle() == STYLE_COMPACT)
    {
        addInLine("ID",getKeyStr(),10);
        addInLine("des.",getDescr());
        addInLine("TAB",getTabRef(),2);
        addInLine("co",tp_co.getFlagValue(),4,1);
        addInLine("str",tp_co.getFlagStr(),6);
        addInLine("cu",tp_cu.getFlagValue(),3,1);
        addInLine("str",tp_cu.getFlagStr(),8);
        addInLine("sl",tp_sls.getFlagValue(),5,1);
        addInLine("str",tp_sls.getFlagStr(),3);
        addInLine("Tr",getFlagValue(),6,1);
        moveLineInBlock();
    }
    PReport::reportMake(m_objname);
}

double TempiRitorno::getVr( void ) const
{
    return (m_vr);
}
